<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="lib/layui-v2.5.6/layui/css/layui.css">
    <!--[if IE 6]>
    <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>建材列表</title>
    <link rel="stylesheet" href="lib/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
</head>
<body class="pos-r">
<div class="pos-a"
     style="margin-top:10px;width:200px;left:0;top:0; bottom:0; height:100%; border-right:1px solid #e5e5e5; background-color:#f5f5f5; overflow:auto;">
    <div id="product-category-list"></div>
</div>
<div style="margin-left:200px;">

    <div class="page-container">
        <p id="title"></p>
        <table id="goods-list" lay-filter="goods-filter"></table>
    </div>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="lib/layui-v2.5.6/layui/layui.js"></script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
    </div>
</script>
<script type="text/javascript">
    var layer;
    var goodsIds = [];
    var tableId;
    var catyId = 1;
    var table;
    $(document).ready(function () {
        layui.use(['tree', 'layer', 'table'], function () {
            var tree = layui.tree;
            var treeList = initTree();
            layer = layui.layer;

            table = layui.table;

            //第一个实例
            tableId = table.render({
                elem: '#goods-list'
                , id: "goods-list"
                , url: '/Olala/goods/page' //数据接口
                , where: {
                    catyId: 1
                }
                , method: 'post'
                , page: true //开启分页
                , toolbar: '#toolbar'
                , response: {
                    statusCode: 20
                }
                , cols: [[ //表头
                    {field: 'checkBox', type: 'checkbox', width: '5%'}
                    , {field: 'goodsId', title: '编号', width: '10%'}
                    , {field: 'goodsSn', title: '商品货号', width: '10%'}
                    , {field: 'goodsName', title: '商品名称', width: '15%'}
                    , {field: 'effectiveDate', title: '生产日期', width: '15%'}
                    , {field: 'expiryDays', title: '有效期', width: '5%'}
                    , {field: 'goodsBrief', title: '商品描述', width: '20%'}
                    , {field: 'marketPrice', title: '市场价', width: '5%'}
                    ,
                    {
                        field: 'isOnsale', title: '是否开售', width: '7.5%',
                        templet: function (data) {
                            return data.isOnsale === 1 ? '是' : '否';
                        }
                    }
                    ,
                    {
                        title: '操作', width: '7.5%',
                        templet: function (data) {
                            return '<button class="layui-btn layui-btn-sm" onclick="update(' + data.goodsId + ')" lay-event="update">编辑</button>';
                        }
                    }
                ]]
            });

            table.on('checkbox(goods-filter)', function (obj) {
                if (obj.checked) {
                    goodsIds.push(obj.data.goodsId);
                    console.log(goodsIds)
                } else {
                    for(var i = 0;i<goodsIds.length;i++){
                        if(goodsIds[i] == obj.data.goodsId){
                            goodsIds.splice(i,1)
                        }
                    }
                }
            });

            //监听事件
            table.on('toolbar(goods-filter)', function (obj) {
                switch (obj.event) {
                    case 'add':
                        add();
                        break;
                    case 'delete':
                        del();
                        break;
                }
            });
            //渲染
            var inst1 = tree.render({
                elem: '#product-category-list'  //绑定元素
                , id: "categoryTree"
                , data: treeList
                , click: function (obj) {
                    catyId = obj.data.id;
                    if (obj.data.children.length <= 0) {
                        $("#title").text(obj.data.title);
                        table.reload('goods-list', {
                            url: '/Olala/goods/page'
                            , where: {
                                catyId: obj.data.id
                            }
                        });
                    }
                }
            });

        });


    });

    function del() {
        var index = layer.confirm('确认删除？', {
            btn: ['确认', '取消'], //按钮
            end: function () {
                tableId.reload();
            }
        }, function () {
            var ids = [];
            for (var i = 0; i < goodsIds.length; i++) {
                if (goodsIds[i] != null) {
                    ids.push(goodsIds[i]);
                }
            }
            console.log(goodsIds)
            console.log(ids)
            $.ajax({
                url: "/Olala/goods/delByIds",
                data: {
                    ids: ids
                },
                traditional: true,
                dataType: "json",
                type: "post",
                success: function (result) {
                    layer.msg(result.msg);
                    layer.close(index);
                }
            });
        }, function () {
            layer.close(index);
        });
    }

    function update(goodsId) {
        localStorage.setItem("catyId", catyId);
        localStorage.setItem("goodsId", goodsId);
        var index = layer.open({
            type: 2,
            title: '修改',
            shade: 0.8,
            content: '/Olala/server/product-update.html',
            end: function () {
                tableId.reload();
                localStorage.removeItem("goodsId");
                localStorage.removeItem("catyId");
            }
        });
        layer.full(index);
    }

    function add() {
        localStorage.setItem("catyId", catyId);
        var index = layer.open({
            type: 2,
            title: '添加',
            shade: 0.8,
            content: '/Olala/server/product-add.html',
            end: function () {
                table.reload('goods-list', {
                    url: '/Olala/goods/page'
                    , where: {
                        catyId: localStorage.getItem("catyId")
                    }
                });
                localStorage.removeItem("catyId");
            }
        });
        layer.full(index);
    }

    function initTree() {
        var treeList;
        $.ajax({
            url: "/Olala/category/brandCateGoryToTree",
            type: "post",
            dataType: "json",
            async: false,
            success: function (result) {
                treeList = result.data;

            }
        });
        return treeList;
    }
</script>
</body>
</html>